package apps.AfterDlcs.afterDlcsBL.buff.myBuff.Base;

import apps.AfterDlcs.afterDlcsBL.Event;
import apps.AfterDlcs.afterDlcsBL.buff.Buff;
import apps.AfterDlcs.afterDlcsBL.buff.BuffKeys;
import apps.AfterDlcs.afterDlcsBL.buff.myBuff.Attack.HurtKeys;
import apps.AfterDlcs.afterDlcsBL.hero.HeroKeys;

public class BaseAttackBuff extends Buff
{
    @Override
    public void handleEvent(Event event)
    {
        if (event.key.equals(BuffKeys.NOR_ATK) &&event.fromHero==this.owner)
        {
            if(event.eventTurn==2)
            {
                int ep=this.owner.getIntByEvent(HeroKeys.EP);
                int oneAtkEp=this.owner.getIntByEvent(HeroKeys.NOR_ATK_EP);
                if(ep<oneAtkEp)
                {
                    event.goOn=false;
                    return;
                }
            }
            else if(event.eventTurn == 4)
            {
                double atk=Double.parseDouble(event.fromHero.getValueByEvent(owner, new Object[]{HeroKeys.NORMAL_ATK,"0"}));
                int def=Integer.parseInt(event.toHero.getValueByEvent(event.toHero,new Object[]{HeroKeys.NORMAL_DEFENCE,"0"}));

                for(int i=1;i<=def;i++)
                {
                    atk=atk*0.99;
                }
                for(int i=def;i<0;i++)
                {
                    atk=atk*1.01;
                }
                int hurt=(int) atk;
                if(atk-hurt>=0.5) hurt++;
                this.owner.warSand.handleEvent(new Event(owner,event.toHero,event.toHero,BuffKeys.DO_HURT,new Object[]{hurt+"", HurtKeys.NORMAL_HURT}));
            }
        }
        else  if (event.key.equals(BuffKeys.DO_HURT) && event.eventTurn == 4 &&event.toHero==this.owner)
        {
            this.owner.warSand.handleEvent(new Event(event.fromHero,event.toHero,event.toHero,BuffKeys.DEC_HP,event.values));
        }
        else  if (event.key.equals(BuffKeys.DEC_HP) && event.eventTurn == 4 &&event.toHero==this.owner)
        {
            event.toHero.addNum(HeroKeys.HP,-Integer.parseInt((String)event.values[0]),Integer.parseInt(event.toHero.getValueByEvent(HeroKeys.MAX_HP)),0);
            if(event.toHero.getHealthByEvent(this.owner)<=0)
            {
                this.owner.warSand.handleEvent(new Event(event.fromHero,event.toHero,event.toHero,BuffKeys.KILL,event.values));
            }
        }
        else  if (event.key.equals(BuffKeys.KILL) && event.eventTurn == 4 &&event.toHero==this.owner)
        {
            this.owner.warSand.handleEvent(new Event(event.fromHero,event.toHero,event.toHero,BuffKeys.DIE,event.values));
        }
        else  if (event.key.equals(BuffKeys.DIE) && event.eventTurn == 4 &&event.toHero==this.owner)
        {
            event.toHero.setValue(HeroKeys.ALIVE,"0");
        }
    }
}
